package com.example.dirviesystem.coach.controller;

import com.example.dirviesystem.config.AliOSSutils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collections;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:5173")
public class FileUploadController {

    @Autowired
    private AliOSSutils aliOSSutils;

    @PostMapping("/upload")
    public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body("文件不能为空");
        }
        System.out.println("接收到文件: " + file.getOriginalFilename()); // 调试信息
        try {
            String fileUrl = aliOSSutils.upload(file);
            return ResponseEntity.ok().body(Collections.singletonMap("url", fileUrl));
        } catch (IOException e) {
            return ResponseEntity.status(500).body("上传失败: " + e.getMessage());
        }
    }
}